package liumengbaio.lolzhangyoubao.Activity.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import liumengbaio.lolzhangyoubao.Activity.Adapter.MoreTreasureRecordAdapter;
import liumengbaio.lolzhangyoubao.Activity.InFo.MoreTreasureBodyInfo;
import liumengbaio.lolzhangyoubao.Activity.InFo.MoreTreasureHeadInfo;
import liumengbaio.lolzhangyoubao.Activity.utils.HttpUtils;
import liumengbaio.lolzhangyoubao.Activity.utils.MorePopUtils;
import liumengbaio.lolzhangyoubao.Activity.utils.ScroListView;
import liumengbaio.lolzhangyoubao.R;

/**
 * 商品详情界面
 * Created by lanouhn on 16/9/10.
 */
public class MoreTreasureDetailsActivity extends AppCompatActivity {

    private MorePopUtils mPopupWindow;
    private String id;
    private Button join;
    private ImageView pic, back;
    private ScroListView mListView;
    private ProgressBar mProgressBar;
    private MoreTreasureBodyInfo bodyInfo;
    private MoreTreasureHeadInfo headInfo;
    private MoreTreasureRecordAdapter recordAdapter;
    private RelativeLayout mRelativeLayout, record;
    private TextView name, date, number, surplus, unit, goodAmount;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.more_treasure_details);
        initView();
        initData();
        initEvent();
        getRecordData();
        getDetailsData();
    }

    private void initView() {
        mListView = (ScroListView) findViewById(R.id.more_treasure_listView);
        pic = (ImageView) findViewById(R.id.more_treasure_picture);
        back = (ImageView) findViewById(R.id.more_treasure_back);
        name = (TextView) findViewById(R.id.more_treasure_name);
        date = (TextView) findViewById(R.id.more_treasure_date);
        number = (TextView) findViewById(R.id.more_treasure_number);
        surplus = (TextView) findViewById(R.id.more_treasure_surplus);
        unit = (TextView) findViewById(R.id.more_treasure_unit);
        goodAmount = (TextView) findViewById(R.id.more_treasure_price);
        mProgressBar = (ProgressBar) findViewById(R.id.more_treasure_progressBar);
        mRelativeLayout = (RelativeLayout) findViewById(R.id.more_introdece);
        record = (RelativeLayout) findViewById(R.id.more_record);
        join = (Button) findViewById(R.id.more_treasure_popWindow);
    }

    private void initData() {
        /**
         * 得到上个页面传过来的数据
         */
        Intent intent = getIntent();
        id = intent.getStringExtra("good_id");
        String tepUnit = intent.getStringExtra("unit");
        unit.setText(tepUnit + "/人次");
        String tepCount = intent.getStringExtra("goodAmount");
        goodAmount.setText(tepCount);
    }

    private void initEvent() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MoreTreasureDetailsActivity.this, MoreTreasureIntroduceActivity.class);
                intent.putExtra("url", headInfo.getData().getDesc_url());
                startActivity(intent);
            }
        });
        record.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MoreTreasureDetailsActivity.this, MoreTreasureRecordActivity.class);
                intent.putExtra("record_id", headInfo.getData().getRaid_define_id() + "");
                startActivity(intent);
            }
        });
        join.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupWindow.showAtLocation(pic, Gravity.BOTTOM, 0, 0);
            }
        });
    }

    private void getRecordData() {
        String urlRecord = "http://lol.bet.zhangyoubao.com/service/rest?api=raid.raidrecords&game=lol&platformVersion=400901";
        String urlRecordBody = "apiVersion=v1&platform=android&os=android&osVersion=19&secretSignature=vONdqzIO1g1NpFS" +
                "TxGvyAlyfr%2Bs%3D&game=lol&sign=&platformVersion=400901&time=1473473235&secretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx" +
                "3gnPhESA&nonce=658087&userToken=1285c08b612411d0bfc35f213eb5d92a73&secretVersion=v1.0&userId=15863305&api=raid." +
                "raidrecords&deviceId=JfoZvarCyLfJIqs9SRFolapk21%2FwEw63JVMaX7Y3v5w%3D&params%5Bsort%5D=0&params%5Bgoods_id%5D=" + id + "&\n";
        try {
            HttpUtils.doPostAsyn(urlRecord, urlRecordBody, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    bodyInfo = gson.fromJson(result, MoreTreasureBodyInfo.class);
                }
            }, handler);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void getDetailsData() {
        String urlDetails = " http://lol.bet.zhangyoubao.com/service/rest?api=raid.goodsdetail&game=lol&platformVersion=400901";
        String urlDetailsBody = "apiVersion=v1&platform=android&os=android&osVersion=19&secretSignature=T4VT1pQeVNTgSUWCh9amBh1" +
                "a5d4%3D&game=lol&sign=&platformVersion=400901&time=1473473235&secretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3gnPhESA&nonce=3923" +
                "35&userToken=1285c08b612411d0bfc35f213eb5d92a73&secretVersion=v1.0&userId=15863305&api=raid.goodsdetail&deviceId=JfoZva" +
                "rCyLfJIqs9SRFolapk21%2FwEw63JVMaX7Y3v5w%3D&params%5Bgoods_id%5D=" + id + "&";
        try {
            HttpUtils.doPostAsyn(urlDetails, urlDetailsBody, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    headInfo = gson.fromJson(result, MoreTreasureHeadInfo.class);
                }
            }, handler1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * popwindow中的点击事件
     */
    private View.OnClickListener click = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.more_pop_back:
                    mPopupWindow.dismiss();
                    break;
                case R.id.more_treasure_delete:
                    mPopupWindow.delete();
                    break;
                case R.id.more_treasure_add:
                    mPopupWindow.add();
                    break;
                case R.id.more_pop_one:
                    mPopupWindow.addOne();
                    break;
                case R.id.more_pop_two:
                    mPopupWindow.addTwo();
                    break;
                case R.id.more_pop_three:
                    mPopupWindow.addThree();
                    break;
            }
        }
    };

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    recordAdapter = new MoreTreasureRecordAdapter(MoreTreasureDetailsActivity.this, bodyInfo);
                    mListView.setAdapter(recordAdapter);
                    break;
                case 1:
                    Toast.makeText(MoreTreasureDetailsActivity.this, "数据获取失败！", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    private Handler handler1 = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    /**
                     * 设置本页面的数据
                     */
                    Picasso.with(MoreTreasureDetailsActivity.this).load(headInfo.getData().getGoods_url()).into(pic);
                    name.setText(headInfo.getData().getName());
                    date.setText(headInfo.getData().getSequence());
                    number.setText("总需人次 " + headInfo.getData().getRaid_amount() + "");
                    surplus.setText(headInfo.getData().getRaid_remain() + "");
                    mProgressBar.setMax(headInfo.getData().getRaid_amount());
                    mProgressBar.setProgress(headInfo.getData().getRaid_total());
                    mPopupWindow = new MorePopUtils(MoreTreasureDetailsActivity.this, click, headInfo.getData());
                    break;
                case 1:
                    Toast.makeText(MoreTreasureDetailsActivity.this, "数据获取失败！", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
}
